// api/workspaces/workspace.model.js
import mongoose from "mongoose";
import localDatabase from "../../common/localDatabase.js";

const schema = new localDatabase.Schema(
  {
    name: {
      type: String,
      required: true,
      trim: true
    },
    slug: {
      type: String,
      trim: true
    },
    description: {
      type: String,
      default: ""
    },
    timezone: {
      type: String,
      default: "UTC"
    },
    filePath: {
      type: String,
      default: null
    },
    fileType: {
      type: String,
      default: null
    },
    fileSize: {
      type: Number,
      default: 0
    },
    metadata: {
      type: Object,
      default: {}
    },
    isPublic: {
      type: Boolean,
      default: false
    },
    accountId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Account",
      required: true
    },
    ownerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true
    },
    
    // Keeping original userId field for backward compatibility
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true
    },
    
    // Nested structures
    members: [{
      userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
      name: { type: String },
      email: { type: String },
      role: { type: String, enum: ["admin", "user", "guest"], default: "user" },
      joinedAt: { type: Date, default: Date.now }
    }],
    
    contacts: [{
      contactId: { type: mongoose.Schema.Types.ObjectId },
      name: { type: String },
      email: { type: String },
      phone: { type: String },
      company: { type: String },
      tags: [{ type: String }],
      status: { type: String },
      assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
      createdAt: { type: Date, default: Date.now }
    }],
    
    projects: [{
      projectId: { type: mongoose.Schema.Types.ObjectId },
      name: { type: String },
      description: { type: String },
      status: { type: String },
      contacts: [{ type: mongoose.Schema.Types.ObjectId }],
      assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
      dueDate: { type: Date },
      createdAt: { type: Date, default: Date.now }
    }],
    
    meetings: [{
      meetingId: { type: mongoose.Schema.Types.ObjectId },
      title: { type: String },
      description: { type: String },
      contactIds: [{ type: mongoose.Schema.Types.ObjectId }],
      participantIds: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
      projectId: { type: mongoose.Schema.Types.ObjectId },
      start: { type: Date },
      end: { type: Date },
      location: { type: String },
      type: { type: String },
      createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" }
    }],
    
    notes: [{
      noteId: { type: mongoose.Schema.Types.ObjectId },
      content: { type: String },
      createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
      linkedTo: {
        type: { type: String },
        id: { type: mongoose.Schema.Types.ObjectId }
      },
      createdAt: { type: Date, default: Date.now }
    }],
    
    tasks: [{
      taskId: { type: mongoose.Schema.Types.ObjectId },
      title: { type: String },
      description: { type: String },
      dueDate: { type: Date },
      assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
      status: { type: String },
      projectId: { type: mongoose.Schema.Types.ObjectId },
      contactId: { type: mongoose.Schema.Types.ObjectId },
      createdAt: { type: Date, default: Date.now }
    }],
    
    teams: [{
      _id: { type: mongoose.Schema.Types.ObjectId, ref: "Team" },
      code: { type: String },
      name: { type: String }
    }],
    
    lastAccessed: {
      type: Date,
      default: null
    },
    
    // MySQL connection fields (kept from our previous changes)
    mysqlHost: {
      type: String,
      default: null
    },
    mysqlPort: {
      type: Number,
      default: 3306
    },
    mysqlUser: {
      type: String,
      default: null
    },
    mysqlPassword: {
      type: String,
      default: null
    },
    mysqlDatabase: {
      type: String,
      default: null
    },
    mysqlTable: {
      type: String,
      default: null
    },
    mysqlConnectionType: {
      type: String,
      enum: ['direct', 'ssh', null],
      default: null
    }
  },
  { timestamps: true, toJSON: { virtuals: true } }
);

schema.virtual("id").get(function () {
  return this._id;
});

// Pre-save hook to generate slug if not provided
schema.pre('save', function(next) {
  if (!this.slug && this.name) {
    this.slug = this.name.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\-]+/g, '');
  }
  
  // If userId is provided but ownerId is not, use userId as ownerId
  if (this.userId && !this.ownerId) {
    this.ownerId = this.userId;
  }
  
  next();
});

const Workspace = localDatabase.model("Workspace", schema, "workspace");

export default Workspace;